package com.vfa.vocaburay;

import java.util.ArrayList;

import com.vfa.data.Category;
import com.vfa.data.Language;
import com.vfa.data.QuizName;
import com.vfa.data.QuizWordsData;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class SettingQuizOption extends Activity {
	EditText username, numOfquestion;
	Spinner spQuizname, spLanguage, spCatagory;
	Button Setting;
	String user, quizname;
	int numberQuestion = 10;
	private SharedPreferences prefs;
	public static final String prefName = "PrefOfQuiz";
	public static final String USER = "user";
	public static final String NUMBERQS = "numberQuiz";
	public static final String QUIZNAME = "QuizName";
	QuizWordsData quizdata;
	ArrayList<QuizName> arrQuizname;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_quizoption);
		
		username = (EditText) findViewById(R.id.settingUser);
		numOfquestion = (EditText) findViewById(R.id.SettingnoOfQuestions);
		spQuizname = (Spinner) findViewById(R.id.SpQuizName);
		Setting = (Button) findViewById(R.id.btSaveSetting);
		
		quizdata = new QuizWordsData(this);
		arrQuizname = new ArrayList<QuizName>();
		arrQuizname = quizdata.getAllQuizName();
		
		ArrayAdapter<QuizName> adapQuizname = new ArrayAdapter<QuizName>(this, R.layout.listiterm, arrQuizname);
		adapQuizname.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spQuizname.setAdapter(adapQuizname);
		
		spQuizname.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				quizname = spQuizname.getSelectedItem().toString();
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
			
		LoadSharePreferences();
		
		Setting.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				prefs = getSharedPreferences(prefName, MODE_PRIVATE);
				SharedPreferences.Editor editor = prefs.edit();
				user = username.getText().toString();
				numberQuestion = Integer.parseInt(numOfquestion.getText().toString());
	
				try{
					editor.putString(USER, user);
					editor.putInt(NUMBERQS, numberQuestion);
					editor.putString(QUIZNAME, quizname);
					editor.commit();
					
				}catch (Exception e) {
					// TODO: handle exception
				}
				SettingQuizOption.this.finish();
				
			}
		});
	}
	
	private void LoadSharePreferences(){
		SharedPreferences shrarePrefs = getSharedPreferences(prefName, MODE_PRIVATE);
		user = shrarePrefs.getString(USER, "Admin");
		username.setText(user);
		quizname = shrarePrefs.getString(QUIZNAME, spQuizname.getSelectedItem().toString());
		numberQuestion = shrarePrefs.getInt(NUMBERQS, 10);
		numOfquestion.setText(String.valueOf(numberQuestion));
		setPositionSpinerQuizname();
		
	}
	
	private void setPositionSpinerQuizname(){
		for(int i = 0;  i < arrQuizname.size(); i++){
			if(spQuizname.getItemAtPosition(i).toString().equals(quizname)){
				spQuizname.setSelection(i);
			}
		}
	}
	
	/*private void setPositionSpinerCatalory(){
		for(int i = 0; i < categoryList.size(); i++){
			if(spCatagory.getItemAtPosition(i).toString().equals(catagori)){
				spCatagory.setSelection(i);
			}
		}
	}*/
	
}
